<?php

namespace Common\Model;

/**
 * Description of AdminAuthorityModel
 * 
 * @author songanwei
 * @date 2015-12-31 
 */
class AdminAuthorityModel extends BaseModel {

    protected $trueTableName = 'tb_admin_authority';

    /**
     * 返回用户客栈所有数据
     * @modify songanwei 2016-01-04
     * @param integer $userId  用户ID
     * @param integer/array $hotelIds  客栈ID或客栈ID数组
     * @return type
     */
    public function getInfosByAdminHotel($userId, $hotelIds) {
        $where['adminlogin_id'] = $userId;
        if (is_array($hotelIds)) {
            $where['hotelentity_id'] = array('in', $hotelIds);
        } else {
            $where['hotelentity_id'] = $hotelIds;
        }
        return $this->where($where)->select();
    }

    /**
     * 
     * @param integer $adminId  管理员ID
     * @param integer $hotelId 客栈ID
     * @param integer $authorityId 权限ID
     * @return type
     */
    public function getInfoByAdminHotelAuthority($adminId, $hotelId, $authorityId) {
        $where = array(
            'adminlogin_id' => $adminId,
            'hotelentity_id' => $hotelId,
        );
        if(empty($authorityId))
        {
            return $this->where($where)->select();
        }
        if(is_array($authorityId))
        {
            $where['hotelauthority_id'] = array('in', $authorityId);
            return $this->where($where)->select();
        }
        $where['hotelauthority_id'] = $authorityId;

        if (is_array($hotelId)) {
            $where['hotelentity_id'] = array('in', $hotelId);
            return $this->where($where)->select();
        }

        return $this->where($where)->find();
    }

    /**
     * 
     * @param integer $adminId  管理员ID
     * @param integer $hotelId 客栈ID
     * @param integer $hotelAuthorityId 客栈权限ID
     * @return type
     */
    public function addInfo($adminId, $hotelId, $hotelAuthorityId) {
        $options = array(
            'adminlogin_id' => $adminId,
            'hotelentity_id' => $hotelId,
            'hotelauthority_id' => $hotelAuthorityId
        );
        return $this->add($options);
    }

    /**
     * 删除管理员客栈权限
     * @modify songanwei 2015-12-31
     * @param integer $adminId  管理员ID
     * @param integer $hotelId 客栈ID
     * @param integer $hotelAuthorityId 客栈权限ID
     * @return type
     */
    public function deleteInfoByAdminHotelAuthority($userId, $hotelId, $hotelAuthorityId) {
        $where = array(
            'adminlogin_id' => $userId,
            'hotelentity_id' => $hotelId,
            'hotelauthority_id' => $hotelAuthorityId
        );
        $result = $this->where($where)->delete();
        return $result === false ? false : true;
    }

    /**
     * 
     * @param integer $userId  客栈管理员ID
     * @param integer/array $hotelId 客栈ID或客栈ID数组
     * @return type
     */
    public function deleteInfoByAdminHotel($userId, $hotelId) {
        $where = array(
            'adminlogin_id' => $userId,
        );
        if (is_array($hotelId)) {
            $where['hotelentity_id'] = array('in', $hotelId);
        } else {
            $where['hotelentity_id'] = $hotelId;
        }
        $result = $this->where($where)->delete();
        return $result === false ? false : true;
    }

}
